package com.chao.yyks.modular.sys.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chao.common.util.ListUtile;
import com.chao.common.util.RequestUtile;
import com.chao.common.util.StrUtile;
import com.chao.common.util.exception.SysException;
import com.chao.yyks.modular.sys.bean.EnClasss;
import com.chao.yyks.modular.sys.bean.EnClasssType;
import com.chao.yyks.modular.sys.mapper.EnClasssMapper;
import com.chao.yyks.modular.sys.mapper.EnClasssTypeMapper;
import com.chao.yyks.modular.sys.mapper.EnStudentMapper;
import com.chao.yyks.modular.sys.service.EnClasssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.chao.common.util.reaction.PageList;
import java.util.Date;
import java.util.List;


/**
 * 班级ServiceImpl
 * chao
 * 2024-09-09
 */
@Service
public class EnClasssServiceImpl implements EnClasssService {

	/**
	 * 班级Mapper
	 */
	@Autowired
	private EnClasssMapper enClasssMapper;

	/**
	 * 问题数量配置Mapper
	 */
	@Autowired
	private EnClasssTypeMapper enClasssTypeMapper;

	/**
	 * 学生Mapper
	 */
	@Autowired
	private EnStudentMapper enStudentMapper;

	/**
	 * 新增班级
	 */
	@Override
	@Transactional
	public void insertEnClasss(EnClasss enClasss){
		enClasss.setClasssId(StrUtile.getId());
		enClasss.setCtime(new Date());
		enClasss.setCreateUserId(RequestUtile.getLoginUserId());
		enClasssMapper.insertEnClasss(enClasss);

//		String s = "[{\"problemUpType\":\"1\",\"problemType\":\"20240912102619086523\",\"problemNum\":12,\"problemScore\":\"1.2\"},{\"problemUpType\":\"1\",\"problemType\":\"20240921182320329493\",\"problemNum\":12,\"problemScore\":\"1.2\"},{\"problemUpType\":\"2\",\"problemType\":\"30240912165640980708\",\"problemNum\":5,\"problemScore\":\"2\"},{\"problemUpType\":\"2\",\"problemType\":\"10240912165618224731\",\"problemNum\":10,\"problemScore\":\"0.5\"},{\"problemUpType\":\"2\",\"problemType\":\"20240912165629554986\",\"problemNum\":10,\"problemScore\":\"1\"},{\"problemUpType\":\"2\",\"problemType\":\"30240912165640980707\",\"problemNum\":5,\"problemScore\":\"2\"},{\"problemUpType\":\"2\",\"problemType\":\"40240912165652307424\",\"problemNum\":45,\"problemScore\":\"0.2\"},{\"problemUpType\":\"4\",\"problemType\":\"20240912165711537366\",\"problemNum\":2,\"problemScore\":\"3\"},{\"problemUpType\":\"4\",\"problemType\":\"20240912165718525693\",\"problemNum\":2,\"problemScore\":\"3\"},{\"problemUpType\":\"5\",\"problemType\":\"20240912165702383873\",\"problemNum\":1,\"problemScore\":\"10\"},{\"problemUpType\":\"6\",\"problemType\":\"20240912165731425880\",\"problemNum\":1,\"problemScore\":\"10\"}]";
//		List<EnClasssType> typeList = JSONArray.parseArray(s).toJavaList(EnClasssType.class);
//		enClasss.setTypeList(typeList);

		if(!ListUtile.isEmpty(enClasss.getTypeList())){
			enClasss.getTypeList().forEach(enClasssType -> {
				enClasssType.setClasssId(enClasss.getClasssId());
				enClasssType.setClasssNumId(StrUtile.getId());
			});
			enClasssTypeMapper.insertAllEnClasssType(enClasss.getTypeList());
		}
	}

	/**
	 * 修改班级
	 */
	@Override
	@Transactional
	public void updateEnClasss(EnClasss enClasss){
		EnClasss enClasssPojo = enClasssMapper.selectEnClasssByClasssId(enClasss.getClasssId());
		if(null == enClasssPojo){
		    throw new SysException("修改班级失败:记录不存在!");
		}
		enClasssMapper.updateEnClasss(enClasss);

		enClasssTypeMapper.deleteDataEnClasssType(enClasss.getClasssId());
		if(!ListUtile.isEmpty(enClasss.getTypeList())){
			enClasss.getTypeList().forEach(enClasssType -> {
				enClasssType.setClasssId(enClasss.getClasssId());
				enClasssType.setClasssNumId(StrUtile.getId());
			});
			enClasssTypeMapper.insertAllEnClasssType(enClasss.getTypeList());
		}
	}

	/**
	 * 物理删除班级
	 */
	@Override
	@Transactional
	public void deleteDataEnClasss(String classsId){
		if(!ListUtile.isPageEmpty(enStudentMapper.selectEnStudentByList(JSONObject.parseObject("{\"classsId\":\""+classsId+"\"}")))){
			throw new SysException("当前班级下已经有学生存在,无法删除!");
		}
		enClasssMapper.deleteDataEnClasss(classsId);
	}

	/**
	 * 查询班级详情
	 */
	@Override
	public EnClasss selectEnClasssByClasssId(String classsId){
		EnClasss classs = enClasssMapper.selectEnClasssByClasssId(classsId);
		classs.setTypeList(enClasssTypeMapper.selectEnClasssTypeByList(classsId));
		return classs;
	}

	/**
	 * 查询班级列表
	 */
	@Override
	public PageList<EnClasss> selectEnClasssByList(JSONObject json){
		return enClasssMapper.selectEnClasssByList(json);
	}

	/**
	 * 分页查询班级列表
	 */
	@Override
	public PageList<EnClasss> selectEnClasssByPage(JSONObject json){
		return enClasssMapper.selectEnClasssByList(StrUtile.getPageJson(json));
	}

}